﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Net;
using System.IO;

namespace MOWebApplication.ImageHandler
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            try
            {
                {
                    string usrName = context.Request.QueryString["usrName"];
                    string imgName = //"MOclient";//
                        context.Request.QueryString["imgName"];

                   // FileStream fs = File.OpenRead(@"c:\Inetpub\Scripts\IMSAdminUsrImages\alex\alex_5062_20.jpg");
                    FileStream fs = File.OpenRead(@"c:\Inetpub\Scripts\IMSAdminUsrImages\"+ usrName + @"\" + imgName + ".jpg");
                   
                    byte[] data = new byte[fs.Length];
                    fs.Read(data, 0, data.Length);
                    context.Response.BinaryWrite(data);

                    DirectoryInfo dir = new DirectoryInfo(@"c:\Inetpub\Scripts\IMSAdminUsrImages\" + usrName);
                    foreach(FileInfo f in dir.GetFiles())
                    {
                     if(f.LastWriteTime < DateTime.Now.AddMinutes(-5))
                     {
                     f.Delete();
                     }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
